package com.yy.permissions_general.module.system.user.controller;

import com.yy.permissions_general.base.controller.BaseController;
import com.yy.permissions_general.common.aopSubmit.aop.NoRepeatSubmit;
import com.yy.permissions_general.common.constant.SystemConstant;
import com.yy.permissions_general.module.system.aopLog.aop.MyLog;
import com.yy.permissions_general.module.system.user.entity.User;
import com.yy.permissions_general.module.system.user.service.UserService;
import com.yy.permissions_general.utils.ResultUtil;
import com.yy.permissions_general.utils.encrypt.AESUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 用户前端控制器
 * </p>
 *
 * @author YY
 * @since 2021-10-10
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    @Resource
    private UserService userService;

    /**
    * 查询列表
    * @param user
    * @param limit
    * @param page
    * @return
    */
    @RequestMapping("/list")
    @MyLog("查询用户")
    @NoRepeatSubmit(key="user.list")
    public ResultUtil list(User user,@RequestParam(value = "limit",defaultValue = "999999")int limit, @RequestParam(value = "page",defaultValue = "0")int page) {
      return ResultUtil.buildPagination(userService.selectPageList(user,limit,page));
    }

    /**
    * 新增数据
    * @param user
    * @return
    */
    @RequestMapping("/addUser")
    @MyLog("添加用户")
    @NoRepeatSubmit(key="user.addUser")
    @PreAuthorize("hasPermission('/user/addUser','user:addUser')")
    public ResultUtil addUser(User user,@RequestParam(value = "file") MultipartFile file) {
        userService.addUser(user,file);
        return ResultUtil.success();
    }

    /**
     * 修改状态
     * @param user
     * @return
     */
    @RequestMapping("/updateUser")
    @MyLog("修改用户状态")
    @NoRepeatSubmit(key="user.updateUser")
    @PreAuthorize("hasPermission('/user/updateUser','user:updateUser')")
    public ResultUtil updateUser(User user) {
        userService.updateUser(user);
        return ResultUtil.success();
    }

    /**
     * 重置密码
     * @param user
     * @return
     */
    @RequestMapping("/resetUser")
    @MyLog("重置用户密码")
    @NoRepeatSubmit(key="user.resetUser")
    @PreAuthorize("hasPermission('/user/resetUser','user:resetUser')")
    public ResultUtil resetUser(User user) {
        try {
            user.setPassword(AESUtil.aesEncrypt(SystemConstant.PASS_WORD,AESUtil.KEY_STR));
        } catch (Exception e) {
            e.printStackTrace();
        }
        userService.updateUser(user);
        return ResultUtil.success();
    }

    /**
     * 修改密码
     * @param map
     * @return
     */
    @RequestMapping("/updatePassword")
    @MyLog("修改用户密码")
    @NoRepeatSubmit(key="user.updatePassword")
    @PreAuthorize("hasPermission('/user/updatePassword','user:updatePassword')")
    public ResultUtil updatePassword(@RequestParam Map<String,Object> map) {
        userService.updatePassword(map);
        return ResultUtil.success();
    }

    /**
    * 修改数据
    * @param user
    * @return
    */
    @RequestMapping("/editUser")
    @MyLog("修改用户")
    @NoRepeatSubmit(key="user.editUser")
    @PreAuthorize("hasPermission('/user/editUser','user:editUser')")
    public ResultUtil editUser(User user,@RequestParam(value = "file") MultipartFile file) {
        userService.editUser(user,file);
        return ResultUtil.success();
    }

    /**
    * 查看详情
    * @param id
    * @return
    */
    @RequestMapping("/detailUser")
    @MyLog("查询用户信息")
    public ResultUtil detailUser(Integer id) {
        User user = userService.getDetailsById(id);
        return ResultUtil.success(user);
    }

    /**
    * 根据ID删除记录
    * @param id
    * @return
    */
    @RequestMapping("/delete")
    @MyLog("删除用户")
    @PreAuthorize("hasPermission('/user/delete','user:delete')")
    public ResultUtil delete(Integer id) {
        userService.deleteById(id);
        return ResultUtil.success();
    }

    /**
    * 批量删除数据
    * @param ids ID集合
    * @return
    */
    @RequestMapping("/deleteBatch")
    @MyLog("批量删除用户")
    @PreAuthorize("hasPermission('/user/deleteBatch','user:deleteBatch')")
    public ResultUtil deleteBatch(@RequestParam(value = "ids[]",required = true) List<Long> ids) {
        userService.deleteBatch(ids);
        return ResultUtil.success();
    }

}
